package com.xujl.mvcbind.ui.refresh.view;

import android.webkit.WebView;
import android.widget.LinearLayout;

import com.just.agentweb.AgentWeb;
import com.just.agentweb.WebChromeClient;
import com.just.agentweb.WebViewClient;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.xujl.fastlib.base.BaseView;
import com.xujl.fastlib.base.IControl;
import com.xujl.fastlib.base.LayoutBuilder;
import com.xujl.mvcbind.R;
import com.xujl.mvcbind.databinding.FragmentRefreshWebBinding;
import com.xujl.mvcbind.databinding.NormalStatusBarBinding;
import com.xujl.mvcbind.widget.CoolIndicatorLayout;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;


/**
 * <pre>
 *     author : xujl
 *     e-mail : 597355068@qq.com
 *     time   : 2019/08/20
 *     desc   :
 *     version: 1.0
 * </pre>
 */

public class RefreshWebFragmentView extends BaseView<FragmentRefreshWebBinding> implements OnRefreshListener {
    private NormalStatusBarBinding mBarBinding;
    private AgentWeb mAgentWeb;

    @Override
    public LayoutBuilder configLayout () {
        return super.configLayout().setToolbarId(R.layout.normal_status_bar);
    }

    @Override
    public void init (IControl control) {
        mBarBinding = getToolbarBinding();
        mBarBinding.ivBack.setOnClickListener(control);
        mBarBinding.ivMenu.setOnClickListener(control);
        CoolIndicatorLayout coolIndicator = new CoolIndicatorLayout(control.exposeContext());
        mAgentWeb = AgentWeb.with((Fragment) control)
                .setAgentWebParent(mBind.llWeb, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT))
                .setCustomIndicator(coolIndicator)
                .setWebChromeClient(new WebChromeClient() {
                    @Override
                    public void onReceivedTitle (WebView view, String webTitle) {
                        super.onReceivedTitle(view, webTitle);
                        mBarBinding.tvTitle.setText(webTitle);
                    }

                })
                .setWebViewClient(new WebViewClient() {
                    @Override
                    public void onPageFinished (WebView view, String url) {
                        super.onPageFinished(view, url);
                        mBind.refreshLayout.finishRefresh();
                    }
                })
                .createAgentWeb()
                .ready()
                .go("https://www.baidu.com/");
        mBind.refreshLayout.setOnRefreshListener(this);

    }


    public boolean onBackPressedSupport () {
        if (mAgentWeb.getWebCreator().getWebView().canGoBack()) {
            mAgentWeb.getWebCreator().getWebView().goBack();
            return true;
        }
        return false;
    }


    @Override
    public void onPause () {
        super.onPause();
        if (mAgentWeb != null) {
            mAgentWeb.getWebLifeCycle().onPause();
        }
    }

    @Override
    public void onResume () {
        super.onResume();
        if (mAgentWeb != null) {
            mAgentWeb.getWebLifeCycle().onResume();
        }
    }

    @Override
    public void onDestroy () {
        if (mAgentWeb != null) {
            mAgentWeb.getWebLifeCycle().onDestroy();
        }
        super.onDestroy();
    }

    @Override
    public void onRefresh (@NonNull RefreshLayout refreshLayout) {
        mAgentWeb.getUrlLoader().reload();
    }
}
